<?php
$r=St_Cdx::getUserInfo();
var_dump($r);
class St_Network
{
    /**
     * @function 远程请求
     * @param $url
     * @param string $postfields
     * @param string $method
     * @param array $headers
     * @return mixed
     */
    static function http($url, $postfields='', $method='GET', $headers=array())
    {
        if(strpos($url,'http') === false)
        {
           return;
        }

        $ci=curl_init();
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        curl_setopt ($ci, CURLOPT_HTTPHEADER, $headers);
        if($method=='POST')
        {
            curl_setopt($ci, CURLOPT_POST, TRUE);
            if($postfields!='')
            {
                curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
            }
        }

        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        return $response;
    }

}
class St_Cdx{    
    private static $headers = ["Content-type: application/json","Authorization: Bearer b8fe75bb-c7e0-4093-9353-767bae2b49b6"];

    /**
     * 获取用户信息
     */
    public static function getUserInfo()
    {        
        $url="https://wx.taishangh.com/api/WeChat/memberInfo";
        $result = St_Network::http($url,"","POST",self::$headers);  
        var_dump($result);exit; 
        return json_decode($result,true);        
    }

    /**
     * @function 获取航班
     * 山咀：9ad5e36c18454145859f2d3d55092f41
     * 三洲：91f35176f4114de48c9434cae74c7007
     * 独湾：ae92da5acdb846d087a6bb998dcc2233     
     */
    public static function getSches($portsId,$sailDate)
    {        
        $url="https://wx.taishangh.com/api/cd/getSches";
        $body=[
            "startPortId"=>$portsId[0],
            "endPortId"=>$portsId[1],
            "sailDate"=>$sailDate
        ];
        $result = St_Network::http($url,json_encode($body,JSON_UNESCAPED_UNICODE),'POST',$headers);   
        return json_decode($result,true);        
    }

    private static function getPortId($suitid){
        $startPortId = '';
        $endPortId = '';
        if(in_array($suitid,[150,156])){
            $startPortId = '9ad5e36c18454145859f2d3d55092f41';
            $endPortId = '91f35176f4114de48c9434cae74c7007';
        }elseif(in_array($suitid,[152,155])){
            $startPortId = '91f35176f4114de48c9434cae74c7007';
            $endPortId = '9ad5e36c18454145859f2d3d55092f41';
        }elseif(in_array($suitid,[154,157])){
            $startPortId = '9ad5e36c18454145859f2d3d55092f41';
            $endPortId = 'ae92da5acdb846d087a6bb998dcc2233';
        }elseif(in_array($suitid,[153,158])){
            $startPortId = 'ae92da5acdb846d087a6bb998dcc2233';
            $endPortId = '9ad5e36c18454145859f2d3d55092f41';
        }
        return [$startPortId,$endPortId];
    }
    /**
     * 获取乘客列表
     */
    public static function searchPassengerInfo($memberId)
    {        
        $url="https://wx.taishangh.com/api/PassengerInfo/searchPassengerInfo/{$memberId}";
        $result = St_Network::http($url,"",'POST',$headers);   
        return json_decode($result,true);        
    }

    /**
     * 添加乘客
     */
    public static function updatePassengerInfo($memberId,$realName,$idNumber,$phone)
    {        
        $url="https://wx.taishangh.com/api/PassengerInfo/updatePassengerInfo";
        $body=[
            "memberinfoId"=>$memberId,
            "realName"=>$realName,
            "idType"=>"01",
            "idNumber"=>$idNumber,
            "phone"=>$phone,
            "birthday"=>date("Y-m-d"),
            "gender"=>1
        ];
        $result = St_Network::http($url,json_encode($body,JSON_UNESCAPED_UNICODE),'POST',$headers);   
        return json_decode($result,true);        
    }

    /**
     * 提交订单
     */
    public static function createBusBoatOrder($schesInfo,$tourers)
    {        
        $url="https://wx.taishangh.com/api/cd/createBusBoatOrder";
        $body=[
            'date' => date("Y-m-d"),            
            'currency' => 'RMB',
            'contact' => $tourers[0]['tourername'],
            'phone' => $tourers[0]['mobile'],
            'payType' => 2,
            'saleChannel' => '小程序',
            'orderType' => 1,
            'lineId' => $schesInfo['lineId'],
            'lineCode' => $schesInfo['lineCode'],
            'segmentId' => $schesInfo['segmentId'],
            'flightId' => $schesInfo['flightId'],
            'flightCode' => $schesInfo['flightCode'],
            'setoffDate' => $schesInfo['setoffDate'],
            'setoffTime' => $schesInfo['setoffTime'],
            'endsetoffTime' => $schesInfo['endsetoffTime'],  
            'flightInfoId' => $schesInfo['flightInfoId'],
        ];
        
        $ticketInfo=[];
        
        $info1 = self::getTicketInfo($schesInfo,$tourers,1);
        if($info1){
            array_push($ticketInfo,$info1);            
        } 
        $info2 = self::getTicketInfo($schesInfo,$tourers,2);
        if($info2){
            array_push($ticketInfo,$info2);            
        }
        $info3 = self::getTicketInfo($schesInfo,$tourers,3);
        if($info3){
            array_push($ticketInfo,$info3);            
        }
        $info4 = self::getTicketInfo($schesInfo,$tourers,4);
        if($info4){
            array_push($ticketInfo,$info4);            
        }
        $info5 = self::getTicketInfo($schesInfo,$tourers,5);
        if($info5){
            array_push($ticketInfo,$info5);            
        } 
        $totalNum = 0;
        $totalMoney=0;
        $realMoney=0;
        foreach($ticketInfo as $ticket){
            $totalNum += $ticket['buyNum'];
            $totalMoney += $ticket['salePrice'];
            $realMoney += $ticket['realMoney'];            
        }
        $body['totalNum']=$totalNum;
        $body['totalMoney']=$totalMoney;
        $body['realMoney']=$realMoney;
        $body['ticketInfo']=$ticketInfo;  
        $result = St_Network::http($url,json_encode($body,JSON_UNESCAPED_UNICODE),'POST',$headers);   
        return json_decode($result,true);        
    }
    private static function getTicketInfo($schesInfo,$tourers,$type){
        $tourerList = [];
        foreach($tourers as $item){
            if($item['type']==$type){
                array_push($tourerList,$item);
            }
        }
        if(count($tourerList)>0){
            $ticketName = self::getTicketName($type);
            $cabinInfo = self::getCabinInfo($schesInfo['cabinInfo'],$ticketName);
            $ticket = [
                'ticketName' => $cabinInfo['ticketName'],
                'ticketId' => $cabinInfo['ticketId'],
                'priceId' => $cabinInfo['priceId'],
                'priceDetailId' => $cabinInfo['priceDetailId'],
                'productId' => $cabinInfo['productId'],
                'supplierId' => $cabinInfo['supplierId'],
                'currency' => $cabinInfo['currency'],
                'retailPrice' => $cabinInfo['retailPrice'],
                'salePrice' => $cabinInfo['salePrice'],
                'realMoney' => $cabinInfo['realMoney'],
                'buyNum' => count($tourerList),
                'fromPort' => $schesInfo['startPortName'],
                'toport' => $schesInfo['endPortName']                
            ];
            $ticket['realNameInfo']=[];
            foreach($tourerList as $tourer){
                $info=[
                    'name' => $tourer['tourername'],
                    'idType' => '01',
                    'idcard' => $tourer['carnumber'],
                    'phone' => $tourer['mobile'],
                ];
                array_push($ticket['realNameInfo'],$info);
            }  
            return $ticket;
        }else{
            return false;
        }        
    }

    private static function getTicketName($type){
        if($type==1){
            $ticketName = "岛民票";
        }elseif($type==2){
            $ticketName = "成人票";
        }elseif($type==3){
            $ticketName = "老人票";
        }elseif($type==4){
            $ticketName = "儿童票";
        }elseif($type==4){
            $ticketName = "岛民儿童票";
        }
        return $ticketName;
    }

    private static function getCabinInfo($cabins,$ticketName){
        foreach($cabins as $item){
            if($item['ticketName']==$ticketName){
                return $item;
            }
        }
    }


    /**
     * 查看交易中的订单
     */
    public static function trading($memberId,$realName,$idNumber,$phone)
    {        
        $url="https://wx.taishangh.com/api/Transport/order/trading";       
        $result = St_Network::http($url,"",'POST',$headers);   
        return json_decode($result,true);        
    }
}
